package com.project.action;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.GeneralManager;
import com.project.entity.ProjectDisplayDetail;
import com.project.entity.SuperAdministrator;
import com.project.service.GeneralManagerManage;

public class GeneralManagerManageAction {
	private GeneralManager generalManager;
	private GeneralManagerManage generalManagerManage;
	private Integer start;
	private Integer limit;
	private Integer generalManageId;
	private String jsonString;// 使用json传递数据
	private String pageMessage1,pageMessage2;
	
	public String addManager(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("super")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		
		
		return "success";
	}
	
	
	public String insertGeneralManager(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("super")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		if(generalManager.getJobnumber() == null || generalManager.getJobnumber().equals("")){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		GeneralManager manager = this.generalManagerManage.getByNumber(String.valueOf(generalManager.getJobnumber()));
		if(manager != null){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		SuperAdministrator admin = (SuperAdministrator)context.getSession().get("admin");
		generalManager.setAuthorizeadministrator(admin.getSuperadministratorid());
		Date d = new Date();
		generalManager.setBecomemanagertime(d);
		int i = this.generalManagerManage.insertGeneralManager(generalManager);
		if(i==1){
			JSONArray jsonObject = JSONArray.fromObject(generalManager);
			this.setJsonString("{success:true,mark : " + "1" + ", number:"+generalManager.getJobnumber()
					+",passward:"+generalManager.getGeneralmanagerpassword() + "}");
			this.generalManager = null;
		}else{
			this.setJsonString("{success:false}");
		}
		generalManager = null;
		return "success";
	}
	
	
	public String getAllGeneralManager(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("super")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		if(start == null || limit == null ){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		List<GeneralManager> generalManager = this.generalManagerManage.getAllGeneralManager(start, limit);
		JSONArray jsonObject = JSONArray.fromObject(generalManager);
		this.setJsonString("{success:true,totalCount : " + this.generalManagerManage.countAllGeneralManager()
				+ ", list:" + jsonObject.toString() + "}");
		return "success";
	}
	
	
	public String updateGeneralManager(){
		ActionContext context = ActionContext.getContext();	
		//判断权限是不是普通管理员登录
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("manager")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		GeneralManager manager = (GeneralManager)context.getSession().get("manager");
		generalManager.setGeneralmanagerid(manager.getGeneralmanagerid());
		int i = this.generalManagerManage.update(generalManager);
		if(i==1){
			JSONArray jsonObject = JSONArray.fromObject(generalManager);
			this.setJsonString("{success:true,totalCount : " + "1" + ", list:"
					+ jsonObject.toString() + "}");
			manager =this.generalManagerManage.getById(manager.getGeneralmanagerid());
			manager.setGeneralmanagerpassword("");
			context.getSession().put("manager", manager);
			context.getSession().put("id", manager.getGeneralmanagerid());
			context.getSession().put("name", manager.getGeneralmanagername());
			context.getSession().put("jobNumber", manager.getJobnumber());
		}else{
			this.setJsonString("{success:false}");
		}
		generalManager = null;
		return "success";
	}
	
	
	//修改密码
	public String changePass(){
		ActionContext context = ActionContext.getContext();	
		//判断权限是不是普通管理员登录
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("manager")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		if(pageMessage1 == null || pageMessage1.equals("") 
				|| pageMessage2 == null || pageMessage2.equals("")){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		GeneralManager manager = (GeneralManager)context.getSession().get("manager");
		int i = this.generalManagerManage.changePass(this.pageMessage1, this.pageMessage2, String.valueOf(manager.getGeneralmanagerid()));
		if(i== -1 ){
			this.setJsonString("{success:false,mark:-1}");
		}else
			this.setJsonString("{success:true,mark:1}");
		this.pageMessage1 = null;
		this.pageMessage2 = null;
		return "success";
	}
	
	/**
	 * 获得session里面的manager的内容
	 * @return
	 */
	public String getGeneralManagerById(){
		ActionContext context = ActionContext.getContext();	
		//判断权限是不是普通管理员登录
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("manager")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		GeneralManager manager = (GeneralManager)context.getSession().get("manager");
		JSONArray jsonObject = JSONArray.fromObject(manager);
		this.setJsonString("{success:true,totalCount : " + "1" + ", list:"
				+ jsonObject.toString() + "}");
		this.generalManager = null;
		return "success";
	}
	
	
	
	/**
	 * getter and
	 * setter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 **/
	public GeneralManager getGeneralManager() {
		return generalManager;
	}
	public void setGeneralManager(GeneralManager generalManager) {
		this.generalManager = generalManager;
	}
	public GeneralManagerManage getGeneralManagerManage() {
		return generalManagerManage;
	}
	public void setGeneralManagerManage(GeneralManagerManage generalManagerManage) {
		this.generalManagerManage = generalManagerManage;
	}
	public Integer getStart() {
		return start;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public Integer getLimit() {
		return limit;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	public Integer getGeneralManageId() {
		return generalManageId;
	}
	public void setGeneralManageId(Integer generalManageId) {
		this.generalManageId = generalManageId;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}


	public String getPageMessage1() {
		return pageMessage1;
	}


	public void setPageMessage1(String pageMessage1) {
		this.pageMessage1 = pageMessage1;
	}


	public String getPageMessage2() {
		return pageMessage2;
	}


	public void setPageMessage2(String pageMessage2) {
		this.pageMessage2 = pageMessage2;
	}
	
}
